<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <my-search-bar @myevent="getValue"></my-search-bar>
    </view>
    <!-- 轮播图 -->
    <swiper indicator-dots autoplay circular :interval="3000">
      <navigator
        :url="'/subpkg/goods-detail/index?goods_id=' + item.goods_id"
        open-type="navigate"
        hover-class="navigator-hover"
        v-for="item in swipers"
        :key="item.goods_id"
      >
        <swiper-item>
          <image :src="item.image_src" mode="scaleToFill" />
        </swiper-item>
      </navigator>
    </swiper>
    <!-- 菜单 -->
    <view class="nav-list">
      <image
        v-for="item in catitems"
        :key="item.name"
        :src="item.image_src"
        class="nav-img"
        mode="scaleToFill"
        @click="goToCategory(item.name)"
      />
    </view>
    <!-- 楼层 -->
    <view>
      <view v-for="item in floorList" :key="item.floor_title.name">
        <!-- 标题图片 -->
        <image
          class="floor-title"
          :src="item.floor_title.image_src"
          mode="scaleToFill"
        />
        <view class="floor-img-box">
          <!-- 左侧图片 -->
          <navigator :url="item.product_list[0].navigator_url">
            <image
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              :src="item.product_list[0].image_src"
              mode="widthFix"
            />
          </navigator>
          <!-- 右侧图片 -->
          <view class="right-img-box">
            <navigator
              :url="subitem.navigator_url"
              open-type="navigate"
              hover-class="navigator-hover"
              v-for="(subitem, index) in item.product_list"
              v-show="index > 0"
              :key="subitem.name"
            >
              <image
                :style="{ width: subitem.image_width + 'rpx' }"
                :src="subitem.image_src"
                mode="widthFix"
              />
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
// 导入混入
import tabBar_badge from "@/mixins/tabBar-badge";

export default {
  mixins: [tabBar_badge],
  data() {
    return {
      // 轮播图数组
      swipers: [],
      //  菜单数据数组
      catitems: [],
      //  楼层的数据列表
      floorList: [],
    };
  },
  onLoad() {
    this.getSwipersData();
    this.getCatitemsData();
    this.getFloorListData();
  },
  methods: {
    async getSwipersData() {
      const res = await uni.$request({
        url: "home/swiperdata",
      });
      if (res.meta.status === 200) {
        this.swipers = res.message;
      }
    },
    async getCatitemsData() {
      const res = await uni.$request({
        url: "home/catitems",
      });
      if (res.meta.status === 200) {
        this.catitems = res.message;
      }
    },
    async getFloorListData() {
      const res = await uni.$request({
        url: "home/floordata",
      });
      if (res.meta.status === 200) {
        res.message.forEach((item) => {
          item.product_list.forEach((subitem) => {
            subitem.navigator_url = `/subpkg/goods-list/index?${
              subitem.navigator_url.split("?")[1]
            }`;
          });
        });

        this.floorList = res.message;
      }
    },
    // 跳转到分类页面
    goToCategory(name) {
      console.log(name);
      uni.switchTab({
        url: "/pages/category/index",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.search-box {
  // 设置定位效果为“吸顶”
  position: sticky;
  // 吸顶的“位置”
  top: 0;
  // 提高层级，防止被轮播图覆盖
  z-index: 999;
}

swiper {
  height: 400rpx;

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
